Vue.prototype.$http=axios;
//每次发送axios请求之前都会加上这个基础路径
axios.defaults.baseURL="http://localhost:8080";

//======================axios的前置拦截器【拦截调用后端的请求====================//
axios.interceptors.request.use(res=>{
    let token = localStorage.getItem("token");
    if(token){
        res.headers["token"] = token;
    }
    return res;
},error => {
    Promise.reject(error)
})

//======================axios的后置拦截器【处理后台登录拦截的结果】====================//
axios.interceptors.response.use(res => {
    //后端响应的是没有登录的信息
    if (false === res.data.success && "noLogin" === res.data.message) {
        localStorage.removeItem("token");
        localStorage.removeItem("logininfo");
        //跳转到登录页面
        location.href = "/index.html";
    }
    return res;
},error => {
    Promise.reject(error)
})

//静态资源拦截器：拦截不发送axios请求的资源
let url = location.href;
let path = url.substring(url.lastIndexOf("/"));
if (path != "/login.html" && path != "/register.html"
    && url.indexOf("/binder.html") == -1  && url.indexOf("/callback.html") == -1){
    //获取localStorage的登录信息
    let logininfo = localStorage.getItem('logininfo');
    if (!logininfo){
        location.href = "/login.html";
    }
}

//将url地址栏解析成一个json对象
function parseUrlParams2Obj(url){
    var obj = {};
    //截取问号?后面的数据
    let params = url.substring(url.indexOf("?")+1);
    //将参数参数分割
    let paramsArr = params.split("&");
    for (var i = 0; i < paramsArr.length; i++){
        //每一个参数
        let param = paramsArr[i];
        let paramArr = param.split("=");
        //对象的属性名 = 属性值
        obj[paramArr[0]] = paramArr[1];
    }
    return obj;
}
